package com.orange.user;

import jakarta.annotation.Resource;
import lombok.var;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;

@SpringBootTest
class UserApplicationTests {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Test
    void contextLoads() {
    }

    @Test
    void testRedisMulti(){
        stringRedisTemplate.opsForValue().set("key1","value1");
        var list = (List)stringRedisTemplate.execute(new SessionCallback() {
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                operations.watch("key1");
                operations.multi();
                operations.opsForValue().increment("key1",1);
                operations.opsForValue().set("key2","value2");
                var value2 = operations.opsForValue().get("key2");
                System.out.println("value2 = " + value2);
                return operations.exec();
            }
        });
        System.out.println("list = " + list);

    }

}
